package com.derive.education_service.web.content.controller;

import com.derive.education_service.common.global.Paper;
import com.derive.education_service.common.global.ResponseMessage;
import com.derive.education_service.web.content.bean.dto.ContentAddDto;
import com.derive.education_service.web.content.bean.dto.ContentUpDto;
import com.derive.education_service.web.content.bean.dto.QueryParam;
import com.derive.education_service.web.content.service.ContentService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.validation.constraints.NotNull;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("content")
@Validated
@Slf4j
public class ContentController {

    @Autowired
    private ContentService contentService;

    /**
     * 添加内容信息 -- 包括资源信息，没有资源则后面再添加也可
     * logo+资源+内容信息
     * @param logoFile
     * @param files
     * @param contentAddDto
     * @return
     */
    @PostMapping("addBusinessContent")
    public ResponseMessage addBusinessContent(@RequestPart("logoFile") MultipartFile logoFile, @RequestPart("files") List<MultipartFile> files, @Validated @RequestPart("contentAddDto") ContentAddDto contentAddDto) {
        return contentService.addBusinessContent(logoFile, files, contentAddDto);
    }

    /**
     * 删除内容信息 -- 存在资源，连带删除资源
     * @param contentId
     * @return
     */
    @PostMapping("delBusinessContent")
    public ResponseMessage delBusinessContent(@NotNull(message = "contentId不能为空") int contentId) {
        return contentService.delBusinessContent(contentId);
    }

    /**
     * 更新内容信息 -- 支持改内容+logo
     * @param file
     * @param contentUpDto
     * @return
     */
    @PostMapping("upBusinessContent")
    public ResponseMessage upBusinessContent(@RequestPart("file") MultipartFile file, @Validated @RequestPart("contentUpDto") ContentUpDto contentUpDto) {
        return contentService.upBusinessContent(file, contentUpDto);
    }

    /**
     * 查询内容详情信息 -- 带内容信息+资源信息
     * @param contentId
     * @return
     */
    @GetMapping("getContentById")
    public ResponseMessage getContentById(@NotNull(message = "contentId不能为空") int contentId) {
        return contentService.getContentById(contentId);
    }

    /**
     * 查询内容列表信息 -- 分页，条件查询(内容资源信息，查询资源详情的时候才需要用)
     * @param queryParam
     */
    @PostMapping("getContentList")
    public ResponseMessage getContentList(@RequestBody QueryParam queryParam) {
        Paper<Map<String, Object>> paper = contentService.getContentList(queryParam);
        return ResponseMessage.createBySuccess(paper);
    }
}
